import requests

def get_weather(city):
    api_key = "YOUR_API_KEY"  # 实际使用时需要申请免费的API key
    base_url = "http://api.openweathermap.org/data/2.5/weather"
    
    try:
        params = {"q": city, "appid": api_key, "units": "metric"}
        response = requests.get(base_url, params=params)
        data = response.json()
        
        if data["cod"] != "404":
            main = data["main"]
            weather = data["weather"][0]
            
            print(f"\n{city}的天气:")
            print(f"温度: {main['temp']}°C")
            print(f"天气状况: {weather['description']}")
            print(f"湿度: {main['humidity']}%")
            print(f"风速: {data['wind']['speed']} m/s")
        else:
            print("城市未找到!")
    except Exception as e:
        print("获取天气信息出错:", e)

city = input("请输入城市名称: ")
get_weather(city)